<template>
  <div class="app-container org">
    <el-card style="width: 100%">
      <!-- 搜索条件盒子 -->
      <p class="pinformation">搜索条件</p>
      <el-form :model="queryParams" ref="queryRef" label-width="80">
        <el-row>
          <el-col :span="4">
            <el-form-item label="文件名称：" prop="fileMc">
              <el-input v-model="queryParams.fileMc" placeholder="请输入文件名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="上传时间：">
              <el-date-picker v-model="dateRange" value-format="YYYY-MM-DD HH:mm:ss" format="YYYY-MM-DD HH:mm:ss"
                type="datetimerange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item>
              <el-button type="success" icon="Search" @click="handleQuery">搜索</el-button>
              <div class="resetBtn" style="margin-left: 6px">
                <el-button icon="Refresh" @click="resetQuery">重置</el-button>
              </div>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <el-row>
        <p class="pinformation" style="margin-top: 8.5px">文件列表</p>
        <p class="totalStyle" style="margin-right: 20px; margin-top: 6px">
          共 {{ total }} 条数据
        </p>
        <el-row style="padding-bottom: 1px; margin-left: 20px">
          <el-button type="success" link icon="Plus" @click="handleAdd">上传文件</el-button>
          <el-button style="color: red !important" link icon="delete" :disabled="!ids.length"
            @click="handledelFile()">批量删除</el-button>
          <el-button type="success" link icon="Plus" :disabled="!ids.length" @click="setPermission">权限设置</el-button>
        </el-row>
      </el-row>

      <el-table class="business" v-loading="loading" :data="fileTable" ref="mainTableRef"
        @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="文件类型" align="center" prop="fileTypeName" width="180" />
        <el-table-column label="名称" align="center" prop="fileMc" />
        <el-table-column label="文件" align="center">
          <template #default="scope">
            <div v-for="(item, index) in scope.row.fileList" :key="index" @click="
              proxy.rowDownloadFile(
                { fileName: item.fileName, fileId: item.fileId },
                1
              )
              " class="fileNameHover">
              {{ item.fileName }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="上传人" align="center" prop="createUserName" width="160" />
        <el-table-column label="上传时间" align="center" prop="createTime" width="160" />
        <el-table-column width="200" label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-button link type="success" @click="editFile(scope.row)">编辑</el-button>
            <el-button link type="success" @click="handledelFile(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize" @pagination="getList" />
      <el-dialog :class="'el-dialog-theme3'" append-to-body draggable :close-on-click-modal="false"
        v-model="open" :title="title" @close="closedDialog" width="600">
        <el-form label-width="55" :model="form" :rules="rules" ref="formRef">
          <el-form-item label="名称：" prop="fileMc">
            <el-input placeholder v-model="form.fileMc" clearable />
          </el-form-item>
          <fileUpload v-model="form.fileList" :limit="5"></fileUpload>
        </el-form>
        <template #footer>
          <div class="dialog-footer">
            <el-button type="success" @click="save">确 定</el-button>
            <el-button type="danger" @click="open = false">取 消</el-button>
          </div>
        </template>
      </el-dialog>
      <el-dialog :class="'el-dialog-theme3'" append-to-body draggable :close-on-click-modal="false"
        v-model="permissionOpen" :title="permissionTitle" @close="permissionClosedDialog" width="600">
        <el-form label-width="55" :model="permissionForm" :rules="permissionRules" ref="permissionFormRef">
          <el-form-item label="角色：" prop="roleIds">
            <el-select clearable v-model="permissionForm.roleIds" style="width: 100%" multiple collapse-tags
              collapse-tags-tooltip :max-collapse-tags="3" filterable>
              <el-option v-for="item in roleList" :key="item.roleId" :label="item.roleName" :value="item.roleId" />
            </el-select>
          </el-form-item>
        </el-form>
        <template #footer>
          <div class="dialog-footer">
            <el-button type="success" @click="handlePermissionSave">确 定</el-button>
            <el-button type="danger" @click="permissionOpen = false">取 消</el-button>
          </div>
        </template>
      </el-dialog>
    </el-card>
  </div>
</template>
  
<script setup name="Record">
import {
  listFile,
  addFile,
  editViewFile,
  delFile,
  permissionSave,
} from "@/api/file/fileAuth";
import { getUser } from "@/api/system/newUser";

const { proxy } = getCurrentInstance();

const dateRange = ref([]);
const queryParams = ref({ pageSize: 10, pageNum: 1, fileType: 40 });
const fileTable = ref([]);
const total = ref(0);
const loading = ref(false);
const ids = ref([]);

const open = ref(false);
const title = ref("");
const form = ref({ fileList: [] });
const rules = ref({
  fileMc: [{ required: true, message: "名称不能为空", trigger: "blur" }],
  fileType: [{ required: true, message: "类型不能为空", trigger: "blur" }],
});

const permissionOpen = ref(false);
const permissionTitle = ref("");
const permissionRules = ref({
  roleIds: [{ required: true, message: "角色不能为空", trigger: "blur" }],
});
const permissionForm = ref({ roleIds: [], dataIds: [] });
const roleList = ref([]);

const getList = () => {
  loading.value = true;
  listFile(proxy.addDateRange(queryParams.value, dateRange.value)).then(
    (res) => {
      fileTable.value = res.rows;
      total.value = res.total;
      loading.value = false;
    }
  );
};

const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

const resetQuery = () => {
  proxy.resetForm("queryRef");
  dateRange.value = [];
  handleQuery();
};

const handleSelectionChange = (selection) => {
  ids.value = selection.map((item) => {
    return item.dataId;
  });
};

getList();

const handleAdd = () => {
  open.value = true;
  title.value = "上传文件";
};

const save = () => {
  proxy.$refs.formRef.validate((valid) => {
    if (valid) {
      form.value.fileType = 40
      addFile(form.value).then((res) => {
        proxy.$modal.msgSuccess("保存成功");
        open.value = false;
        getList();
      });
    }
  });
};

const editFile = (row) => {
  open.value = true;
  title.value = "上传文件";
  editViewFile(row.dataId).then((res) => {
    form.value = res.data;
  });
};

const handledelFile = (row) => {
  proxy.$modal.confirm("确定要删除吗？").then(() => {
    let dataId = row?.dataId || ids.value;
    delFile(dataId).then((res) => {
      proxy.$modal.msgSuccess("删除成功");
      getList();
    });
  });
};

const closedDialog = () => {
  proxy.$refs["formRef"]?.clearValidate();
  form.value = { fileList: [] };
};

const setPermission = () => {
  permissionOpen.value = true;
  permissionTitle.value = "权限设置";
  getUser().then((res) => {
    roleList.value = res.roles;
  });
};

const handlePermissionSave = () => {
  proxy.$refs.permissionFormRef.validate((valid) => {
    if (valid) {
      permissionForm.value.dataIds = ids.value;
      permissionSave(permissionForm.value).then((res) => {
        proxy.$modal.msgSuccess("保存成功");
        permissionOpen.value = false;
      });
    }
  });
};

const permissionClosedDialog = () => {
  proxy.$refs["permissionFormRef"]?.clearValidate();
  permissionForm.value = { roleIds: [], dataIds: [] };
};

watch(
  () => total.value,
  () => {
    if (total.value) {
      heightParams.value = [{ tableName: "main", bottomHeight: 73 }];
    } else {
      heightParams.value = [{ tableName: "main", bottomHeight: 21 }];
    }
    getTableHeight();
  }
);

const heightParams = ref([{ tableName: "main", bottomHeight: 21 }]);

function getTableHeight() {
  proxy.adaptiveHeight(proxy, heightParams.value);
}
onMounted(() => {
  nextTick(() => {
    getTableHeight();
    window.addEventListener("resize", getTableHeight);
  });
});
onUpdated(() => {
  nextTick(function () {
    getTableHeight();
  });
});
onBeforeUnmount(() => {
  window.removeEventListener("resize", getTableHeight);
});
</script>
  